import { create } from 'zustand';
import { AIDialogue } from '@/types';

interface AIState {
  dialogues: AIDialogue[];
  currentDialogue: AIDialogue | null;
  isGenerating: boolean;
  error: string | null;
  generateDialogue: (keywords: string[], difficulty: 'beginner' | 'intermediate' | 'advanced') => Promise<void>;
}

// Mock AI-generated dialogues
const mockDialogues: Record<string, AIDialogue> = {
  'beginner-cat-dog': {
    id: '1',
    keywords: ['cat', 'dog'],
    difficulty: 'beginner',
    english: 'A: Do you have a pet?\nB: Yes, I have a cat and a dog.\nA: What color is your cat?\nB: My cat is black and white.\nA: What about your dog?\nB: My dog is brown. He likes to play.',
    chinese: 'A: 你有宠物吗？\nB: 是的，我有一只猫和一只狗。\nA: 你的猫是什么颜色的？\nB: 我的猫是黑白相间的。\nA: 你的狗呢？\nB: 我的狗是棕色的。它喜欢玩耍。',
    createdAt: new Date().toISOString(),
  },
  'intermediate-school-friend': {
    id: '2',
    keywords: ['school', 'friend'],
    difficulty: 'intermediate',
    english: 'A: How was school today?\nB: It was great! I learned about planets in science class.\nA: Did you see your friends?\nB: Yes, my friend Tom and I played basketball during recess.\nA: That sounds fun. What else did you do?\nB: We had an English test. I think I did well.',
    chinese: 'A: 今天学校怎么样？\nB: 很棒！我在科学课上学习了关于行星的知识。\nA: 你见到你的朋友了吗？\nB: 是的，我和我的朋友汤姆在课间休息时打篮球。\nA: 听起来很有趣。你还做了什么？\nB: 我们进行了英语测试。我觉得我做得不错。',
    createdAt: new Date().toISOString(),
  },
  'advanced-travel-culture': {
    id: '3',
    keywords: ['travel', 'culture'],
    difficulty: 'advanced',
    english: 'A: Have you ever traveled to another country?\nB: Yes, I visited Japan last year. The culture there is fascinating.\nA: What aspects of Japanese culture did you find most interesting?\nB: I was impressed by their attention to detail and the respect they show in daily interactions.\nA: Would you like to experience living in a different culture for an extended period?\nB: Absolutely. I think immersing yourself in another culture broadens your perspective and challenges your assumptions.',
    chinese: 'A: 你曾经去过其他国家旅行吗？\nB: 是的，我去年去了日本。那里的文化很迷人。\nA: 你觉得日本文化的哪些方面最有趣？\nB: 我对他们对细节的关注以及在日常互动中表现出的尊重印象深刻。\nA: 你想体验在不同文化中长期生活吗？\nB: 当然。我认为沉浸在另一种文化中会拓宽你的视野，挑战你的假设。',
    createdAt: new Date().toISOString(),
  }
};

export const useAIStore = create<AIState>()((set, get) => ({
  dialogues: Object.values(mockDialogues),
  currentDialogue: null,
  isGenerating: false,
  error: null,
  
  generateDialogue: async (keywords: string[], difficulty: 'beginner' | 'intermediate' | 'advanced') => {
    set({ isGenerating: true, error: null });
    
    try {
      // Simulate API call to AI service
      await new Promise(resolve => setTimeout(resolve, 2000));
      
      // For demo, select a mock dialogue based on difficulty
      let mockDialogue: AIDialogue;
      
      if (difficulty === 'beginner') {
        mockDialogue = mockDialogues['beginner-cat-dog'];
      } else if (difficulty === 'intermediate') {
        mockDialogue = mockDialogues['intermediate-school-friend'];
      } else {
        mockDialogue = mockDialogues['advanced-travel-culture'];
      }
      
      // Create a new dialogue with the provided keywords
      const newDialogue: AIDialogue = {
        ...mockDialogue,
        id: Math.random().toString(36).substring(2, 9),
        keywords,
        difficulty,
        createdAt: new Date().toISOString(),
      };
      
      const { dialogues } = get();
      
      set({
        dialogues: [newDialogue, ...dialogues],
        currentDialogue: newDialogue,
        isGenerating: false,
      });
    } catch (error) {
      set({
        error: error instanceof Error ? error.message : 'Failed to generate dialogue',
        isGenerating: false,
      });
    }
  },
}));